package com.haobo.controller;

import com.haobo.pojo.Role;
import com.haobo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * ClassName:RoleController
 * Package:com.haobo.controller
 * Description:
 *
 * @Date:2021/5/28 18:07
 * @Author:haobo
 */

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/role_list")
    public ModelAndView role_list(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList = roleService.list();
        //设置模型
        modelAndView.addObject("roleList",roleList);
        //设置试图
        modelAndView.setViewName("role-list");
        return modelAndView;
    }

    @RequestMapping("/role_add")
    public String role_add(Role role){
        roleService.newRole(role);
            //1.转发，是会通过视图解析器，进行前缀后缀添加，可以访问WEB-INF文件夹下的文件。所以我们不用手动添加前缀后缀
            //2.重定向，不会通过视图解析器，也就不会自动添加前缀和后缀，所以我们需要手动添加前缀和后缀。
        return "redirect:/role/role_list";
    }

    @RequestMapping("/delRole/{roleId}")
    public String delRole(@PathVariable("roleId") Long roleId){
        roleService.delRole(roleId);
        return "redirect:/role/role_list";
    }
}
